package com.ywq.app.dao.impl;
import java.util.List;
import java.util.Map;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ywq.app.dao.ArtPicDao;
import com.ywq.app.domain.ArtArtistPic;
import com.ywq.app.domain.ArtAuctionPic;
import com.ywq.app.domain.ArtClass;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.PicBidLog;
import com.ywq.common.exception.AppBusinessException;

public class ArtPicDaoImpl extends SqlSessionDaoSupport implements ArtPicDao {

	private static final Logger logger = LoggerFactory
			.getLogger(ArtPicDaoImpl.class);

	/**
	 * 艺术画总数
	 */
	@Override
	public int getArtPicListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"com.ywq.app.persistence.ArtPicMapper.getArtPicListCount",
					map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicListCount user failure.", e);
			}
			throw new AppBusinessException("getArtPicListCount user failure.",
					e);
		}
	}

	/**
	 * 艺术画列表
	 */
	@Override
	public List<ArtPic> getArtPicList(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return getSqlSession().selectList(
					"com.ywq.app.persistence.ArtPicMapper.getArtPicList", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList list failure.", e);
			}
			throw new AppBusinessException("getArtPicList list failure.", e);
		}
	}

	@Override
	public void insertArtPic(ArtPic artPic) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.insertArtPic",
							artPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic user failure.", e);
			}
			throw new AppBusinessException("insertArtPic user failure.", e);
		}
	}

	// 作品详情
	@Override
	public ArtPic getArtPic(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return (ArtPic) getSqlSession().selectOne(
					"com.ywq.app.persistence.ArtPicMapper.getArtPic", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPic list failure.", e);
			}
			throw new AppBusinessException("getArtPic list failure.", e);
		}
	}

	// 作品分类
	// 根据父ID取得子节点的树结构
	@Override
	public List<ArtClass> getChildById(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"com.ywq.app.persistence.ArtPicMapper.getChildById", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getChannelById database failure.", e);
			}
			throw new AppBusinessException("getChannelById database failure.",
					e);
		}
	}
	//根据频道Id取得频道信息
	@Override
	public ArtClass getClassById(String classId)
			throws AppBusinessException {
		try {
			return  (ArtClass) getSqlSession()
					.selectOne(
							"com.ywq.app.persistence.ArtPicMapper.getClassById",
							classId);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getClassById database failure.", e);
			}
			throw new AppBusinessException("getClassById database failure.",
					e);
		}
	}
	//添加分类
	@Override
	public void addClass(ArtClass artClass) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.addClass",
							artClass);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("addClass database failure.", e);
			}
			throw new AppBusinessException("addClass database failure.", e);
		}
	}
	//更新分类
    @Override
    public void updateClassById(ArtClass artClass) throws AppBusinessException {
        try {
            getSqlSession().update("com.ywq.app.persistence.ArtPicMapper.updateClassById", artClass);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("updateClassById database failure.", e);
            }
            throw new AppBusinessException("updateClassById database failure.",
                    e);
        }
    }
    //定制（艺术家与作品关联表）
    @Override
	public void insertArtArtistPic(ArtArtistPic artArtistPic) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.insertArtArtistPic",
							artArtistPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic user failure.", e);
			}
			throw new AppBusinessException("insertArtPic user failure.", e);
		}
	}
  // 拍卖（拍卖与作品关联表）
    @Override
	public void insertArtAuctionPic(ArtAuctionPic artAuctionPic) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.insertArtAuctionPic",
							artAuctionPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertArtPic user failure.", e);
			}
			throw new AppBusinessException("insertArtPic user failure.", e);
		}
	}
    
    @Override
	public PicBidLog getPicBidLogByPicId(Map<String, Object> map)throws AppBusinessException {

		try {
			return (PicBidLog) getSqlSession().selectOne(
					"com.ywq.app.persistence.ArtPicMapper.getPicBidLogByPicId", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getPicBidLogByPicId list failure.", e);
			}
			throw new AppBusinessException("getPicBidLogByPicId list failure.", e);
		}
	}
    //添加出价纪录
    @Override
	public void insertPicBidLog(PicBidLog newBid) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.insertPicBidLog",newBid);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("insertPicBidLog user failure.", e);
			}
			throw new AppBusinessException("insertPicBidLog user failure.", e);
		}
	}
	//不可以买卖
	@Override
	public void noForwardPic(Map<String, Object> map) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.ArtPicMapper.noForwardPic",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("noForwardPic user failure.", e);
			}
			throw new AppBusinessException("noForwardPic user failure.", e);
		}
	}
	//删除作品
	@Override
	public void deletePic(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.deletePic",identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deletePic user failure.", e);
			}
			throw new AppBusinessException("deletePic user failure.", e);
		}
	}
	//
	@Override
	public void deletePicArtist(String identifier) throws AppBusinessException {
		try {
			getSqlSession()
					.insert("com.ywq.app.persistence.ArtPicMapper.deletePicArtist",identifier);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deletePic user failure.", e);
			}
			throw new AppBusinessException("deletePic user failure.", e);
		}
	}
	
	//更新
	@Override
	public void updatePic(ArtPic artPic) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.ArtPicMapper.updatePic",artPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("noForwardPic user failure.", e);
			}
			throw new AppBusinessException("noForwardPic user failure.", e);
		}
	}
	@Override
	public void updatePicViewAmount(ArtPic artPic) throws AppBusinessException {
		try {
			getSqlSession()
					.update("com.ywq.app.persistence.ArtPicMapper.updatePicViewAmount",artPic);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updatePicViewAmount user failure.", e);
			}
			throw new AppBusinessException("updatePicViewAmount user failure.", e);
		}
	}
	//查询所有分类
	@Override
	public List<ArtClass> getAllClass(Map<String, Object> map) throws AppBusinessException {
		try {
			return  getSqlSession().selectList(
					"com.ywq.app.persistence.ArtPicMapper.getAllClass", map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAllClass database failure.", e);
			}
			throw new AppBusinessException("getAllClass database failure.",
					e);
		}
	}
	@Override
	public int getAllClassCount(Map<String, Object> map) throws AppBusinessException {
		try {
			Object obj = getSqlSession().selectOne(
					"com.ywq.app.persistence.ArtPicMapper.getAllClassCount",
					map);
			return (Integer) (obj == null ? 0 : obj);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAllClassCount user failure.", e);
			}
			throw new AppBusinessException("getAllClassCount user failure.",
					e);
		}
	}
	
}
